package com.kugong;

import com.jfinal.config.*;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.kugong.anchor.Anchor;
import com.kugong.anchor.AnchorController;
import com.kugong.battle.Battle;
import com.kugong.battle.BattleController;
import com.kugong.match.Match;
import com.kugong.match.MatchController;
import com.kugong.point.Point;
import com.kugong.point.PointController;
import com.kugong.signUp.SignUp;
import com.kugong.signUp.SignUpController;
import com.kugong.user.User;
import com.kugong.user.UserController;
import com.kugong.user.Weixin;
import com.kugong.weixin.WeixinApiController;
import com.kugong.weixin.WeixinMsgController;

import java.util.Properties;

/**
 * Created by lenovo on 2015/8/2.
 */
public class CommonConfig extends JFinalConfig{

    public Properties loadProp(String pro, String dev) {
        try {return loadPropertyFile(pro);}
        catch (Exception e)
        {return loadPropertyFile(dev);}
    }

    @Override
    public void configConstant(Constants me) {
        // 如果生产环境配置文件存在，则优先加载该配置，否则加载开发环境配置文件
        loadProp("dbk.properties", "db.properties");
        me.setViewType(ViewType.JSP);
        me.setBaseViewPath("/view");
        me.setDevMode(PropKit.getBoolean("DevMode", false));

        // ApiConfigKit 设为开发模式可以在开发阶段输出请求交互的 xml 与 json 数据
        ApiConfigKit.setDevMode(me.getDevMode());
    }

    @Override
    public void configRoute(Routes me) {
        me.add("/", UserController.class,"/user");
        me.add("/match", MatchController.class);
        me.add("/SignUp", SignUpController.class);
        me.add("/battle", BattleController.class);
        me.add("/point", PointController.class);
        me.add("/anchor", AnchorController.class);
        me.add("/weixin", WeixinMsgController.class);
        me.add("/api", WeixinApiController.class);
    }

    @Override
    public void configPlugin(Plugins me) {
        //c3p0 插件
        C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("jdbcUrl"),PropKit.get("user"),PropKit.get("password").trim());
        me.add(c3p0Plugin);
        //ActiveRecord 插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
        arp.setShowSql(true);
        me.add(arp);
        //映射model
        arp.addMapping("tb_sign_up", SignUp.class);
        arp.addMapping("tb_match", Match.class);
        arp.addMapping("tb_battle", Battle.class);
        arp.addMapping("tb_user", User.class);
        arp.addMapping("tb_point", Point.class);
        arp.addMapping("tb_anchor", Anchor.class);
        arp.addMapping("tb_weixin", Weixin.class);
    }

    @Override
    public void configInterceptor(Interceptors me) {
        // 添加控制层全局拦截器
        me.addGlobalActionInterceptor(new GlobalActionInterceptor());
    }

    @Override
    public void configHandler(Handlers me) {

    }
}
